//
//  WelocomeController.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/22.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit
import SDWebImage

//欢迎用户的界面
class WelocomeController: UIViewController
{

    //懒加载控件
    //(1)懒加载背景图片
    private lazy var backGroundImage:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    //(2)懒加载用户头像
    private lazy var headImageView:UIImageView = {
       
        let imag = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置头像从网络获取头像
        //AuthViewModelTools第一次创建的时候会从本地去解档data数据
        let url:NSURL? = NSURL(string: AuthViewModel.AuthViewModelTools.userModel?.avatar_large ?? "")
        
        if let u = url
        {
            imag.setImageWithURL(u, placeholderImage: UIImage(named: "avatar_default_big"))
        }
       
        //设置为圆形
        imag.layer.cornerRadius = 45
        imag.layer.masksToBounds = true
        return imag
    }()
    
    //(3)懒加载label
    private lazy var wordLabel:UILabel = {
       
        let lab = UILabel()
        
        lab.textColor = UIColor.darkGrayColor()
        
        lab.font = UIFont.systemFontOfSize(18)
        
        let userName = AuthViewModel.AuthViewModelTools.userModel?.screen_name ?? ""
        
        lab.text = "\(userName) 欢迎回来"
        
        lab.textAlignment = .Center
        lab.alpha = 0
        
        return lab
    }()
    
    
    //设置界面的为背景图片界面
    override func loadView()
    {
        view = self.backGroundImage
    }
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //设置界面
        self.setUpUI()
    }
    
    //设置界面
    func setUpUI()
    {
        //界面添加控件
        view.addSubview(self.headImageView)
        view.addSubview(self.wordLabel)
        
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        
        wordLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }

    }
    
    
    //界面已经加载完成之后，开始做动画效果
    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        
        
        //方法2:重新设置头像图片的约束
        //利用update的方式来重新更新头像图片的约束
        self.headImageView.snp_updateConstraints { (make) in
             make.top.equalTo(self.view).offset(100)
        }
        
        //UIView控件的动画效果
        ///
        ///
        /// - parameter <Tduration:             动画时间
        /// - parameter delay:                  演示时间
        /// - parameter usingSpringWithDamping: 阻尼系数（0-1） 阻尼数约大 代表弹性效果越差
        /// - parameter initialSpringVelocity:  加速度
        /// - parameter options:
        /// - parameter animations:
        /// - parameter completion:
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options:[], animations: {
            
            //方法2:对当前的页面进行刷新
            self.view.layoutIfNeeded()
            
            //方法1:直接使用transform
            //self.headImageView.transform = CGAffineTransformMakeTranslation(0, -200)
            //self.wordLabel.transform = CGAffineTransformMakeTranslation(0, -200)
            
            
            }) { (_) in
                
                //上个动画完成之后,label重新显现
                UIView.animateWithDuration(1, animations: {
                    
                    self.wordLabel.alpha = 1
                    
                    }, completion: { (_) in
                        
                     //跳转到主界面
                     UIApplication.sharedApplication().keyWindow?.rootViewController = MainController()
                        
                        
                })
                
        }
        
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    
    

}
